import { _decorator, Component, Node } from 'cc';
import { PrefsManager } from './PrefsManager';
const { ccclass, property } = _decorator;

@ccclass('Tree')
export class Tree extends Component {

    private _trees: Node[][] = [[], [], [], []];

    protected onLoad(): void {
        this.node.children.forEach(element => {
            for (let i = 0; i < element.children.length; i++) {
                this._trees[i].push(element.children[i]);
            }
        });
    }

    protected start(): void {
        this.showTree();
    }

    showTree() {
        let index: number = PrefsManager.Instance.userData.haveWeapon[PrefsManager.Instance.userData.haveWeapon.length - 1];
        if (index >= this._trees.length - 1) {
            index = this._trees.length - 1;
        }
        for (let i = 0; i < this._trees.length; i++) {
            this.changeActive(this._trees[i], false);
        }
        this.changeActive(this._trees[index], true);
    }

    changeActive(nodes: Node[], active: boolean) {
        nodes.forEach(e => {
            e.active = active;
        })
    }
}


